<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1" 
	xmlns:xalan="http://xml.apache.org/xslt" 
	xmlns:exsl="http://exslt.org/common">
	
	<xsl:attribute-set name="base-set">
			<xsl:attribute name="full-name">verbose gc</xsl:attribute>
			</xsl:attribute-set>
	
	<xsl:attribute-set name="x-axis" use-attribute-sets="base-set">
		<xsl:attribute name="x-axis">timestamp</xsl:attribute>
		<xsl:attribute name="x-units">date</xsl:attribute></xsl:attribute-set>
	<xsl:attribute-set name="time-axes" use-attribute-sets="x-axis">
		<xsl:attribute name="y-axis">time</xsl:attribute>
		<xsl:attribute name="y-units">ms</xsl:attribute>
	</xsl:attribute-set>
	<xsl:attribute-set name="space-axes" use-attribute-sets="x-axis">
		<xsl:attribute name="y-units">MB</xsl:attribute>
	</xsl:attribute-set>
<xsl:attribute-set name="time-time-axes" use-attribute-sets="x-axis">
	<xsl:attribute name="x-axis">timestamp</xsl:attribute>
		<xsl:attribute name="x-units">ms</xsl:attribute>
			<xsl:attribute name="y-axis">time</xsl:attribute>
		<xsl:attribute name="y-units">ms</xsl:attribute>
	</xsl:attribute-set>

	<xsl:template match="/">
		<xsl:element name="dataset">
		<xsl:apply-templates select="verbosegc"/>
		</xsl:element>
			</xsl:template>
	<xsl:template match="verbosegc">
		<!-- TODO make this a param -->
		<!-- this could be changed to descendant:: or sys or af -->
		<xsl:variable name="gcs" select="*[name() != 'sys' and gc]"/>
		<!--	<xsl:variable name="gcs" select="*[gc]"/>-->
		<xsl:variable name="times" select="$gcs/gc/timesms"/>
		<variant>
		<run>
		<!-- the element gets defined above, but we want to add a pretty-print name -->
		<xsl:element name="tuple-data" use-attribute-sets="time-axes">
			<xsl:attribute name="label">Mark times</xsl:attribute>
			<xsl:call-template name="analyse-pauses">
				<xsl:with-param name="times" select="$times          >
			</xsl:call-template>
		</xsl:element>
                <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                        <xsl:attribute name="label">GC intervals</xsl:attribute>                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/gc/@intervalms"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                <xsl:attribute name="label">Exclusive access times</xsl:attribute>
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/time/@exclusiveaccessms"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Free heap</xsl:attribute>
                        <xsl:attribute name="y-axis">free heap</xsl:attribute>
                                <xsl:attribute name="y-units">MB</xsl:attribute>                        <xsl:attribute name="colour-multiplier">inverse</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/tenured[2]/@freebytes"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Free LOA</xsl:attribute>
                        <xsl:attribute name="y-axis">free loa heap</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/tenured[2]/loa/@freebytes"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Free SOA</xsl:attribute>
                        <xsl:attribute name="y-axis">free soa heap</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/tenured[2]/soa/@freebytes"/>
                        </xsl:call-template>
                </xsl:element>
                <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Free nursery</xsl:attribute>
                        <xsl:attribute name="y-axis">free nursery</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/nursery[2]/@freebytes"/>
                        </xsl:call-template>
                </xsl:element>
                <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Nursery size</xsl:attribute>
                        <xsl:attribute name="y-axis">nursery size</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/nursery[2]/@totalbytes"/>
                        </xsl:call-template>
                </xsl:element>
                   <xsl:element name="tuple-data" use-attribute-sets="space-axes">
     <xsl:attribute name="label">Tenured area size</xsl:attribute>
                        <xsl:attribute name="y-axis">tenured area size</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/tenured[2]/@totalbytes"/>
                        </xsl:call-template>
                </xsl:element>
                    <xsl:element name="tuple-data" use-attribute-sets="space-axes">
   <xsl:attribute name="label">Bytes flipped</xsl:attribute>
                        <xsl:attribute name="y-axis">flipped</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/gc/flipped/@bytes"/>
                        </xsl:call-template>
                </xsl:element>
                    <xsl:element name="tuple-data" use-attribute-sets="space-axes">
   <xsl:attribute name="label">Bytes tenured</xsl:attribute>
                        <xsl:attribute name="y-axis">tenured</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/gc/tenured[@bytes]/@bytes"/>
                        </xsl:call-template>
                </xsl:element>
                  <xsl:element name="tuple-data" use-attribute-sets="space-axes">
     <xsl:attribute name="label">Free tenured area</xsl:attribute>
                        <xsl:attribute name="y-axis">free tenured area</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/tenured[2]/@freebytes"/>
                        </xsl:call-template>
                </xsl:element>
                <xsl:element name="tuple-data" use-attribute-sets="x-axis">
                <xsl:attribute name="label">Free nursery (ratio)</xsl:attribute>                        <xsl:attribute name="y-axis">free nursery</xsl:attribute>
                        <xsl:attribute name="y-units">%</xsl:attribute>
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/nursery[2]/@percent"/>
                        </xsl:call-template>
                </xsl:element>
                    <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Total nursery size</xsl:attribute>
                        <xsl:attribute name="y-axis">nursery size</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/nursery[2]/@totalbytes"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="x-axis">
                <xsl:attribute name="label">Cards cleaned</xsl:attribute>
                        <xsl:attribute name="y-axis">cards cleaned</xsl:attribute>
                        <xsl:attribute name="y-units">#</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/con/stats/@cardscleaned"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Compacted bytes</xsl:attribute>
                        <xsl:attribute name="y-axis">compacted bytes</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="$gcs/gc/compaction/@movebytes"/>                        </xsl:call-template>
                </xsl:element>
                              <xsl:element name="tuple-data" use-attribute-sets="x-axis">
                <xsl:attribute name="label">Amount traced</xsl:attribute>
                        <xsl:attribute name="y-axis">traced</xsl:attribute>
                        <xsl:attribute name="y-units">%</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/con/stats          @percent"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="space-axes">
                <xsl:attribute name="label">Intended kickoff</xsl:attribute>
                        <xsl:attribute name="y-axis">rate</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes"
                                        select="con/stats/@tracetarget"/>
                        </xsl:call-template>
                </xsl:element>
                <xsl:element name="tuple-data" use-attribute-sets="x-axis">
                <xsl:attribute name="label">Amount traced before concurrent collection completion</xsl:attribute>
                        <xsl:attribute name="y-axis">amount traced before completion</xsl:attribute>
                        <xsl:attribute name="y-units">%</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="con/stats          @percent"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                <xsl:attribute name="label">Actual kickoff</xsl:attribute>
                        <xsl:attribute name="y-axis">rate</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-sizes">
                                <xsl:with-param name="sizes" select="con/stats/@kickoff"/>
                        </xsl:call-template>
                </xsl:element>
                        <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                <xsl:attribute name="label">Cards traced</xsl:attribute>
                        <xsl:attribute name="y-axis">cards traced</xsl:attribute>
                <xsl:attribute name="y-units">#</xsl:attribute>
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="$gcs/con/stats/@traced"/>
                        </xsl:call-template>
                </xsl:element>
                                <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                                <xsl:attribute name="label">Trace rate</xsl:attribute>
                                        <xsl:attribute name="y-axis">cards traced</xsl:attribute>
                <xsl:attribute name="y-units">#</xsl:attribute>
                        <!-- convert the bytes to megabytes -->
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times"
                                        select="con/stats/@tracerate"/>
                        </xsl:call-template>
                </xsl:element>

		<xsl:element name="tuple-data" use-attribute-sets="time-axes">
		<xsl:attribute name="label">Sweep times</xsl:attribute>
			<xsl:call-template name="analyse-pauses">
				<xsl:with-param name="times" select="$times/@sweep"/>
			</xsl:call-template>
		</xsl:element>
		
			<xsl:element name="tuple-data" use-attribute-sets="time-axes">
                <xsl:attribute name="label">Compact times</xsl:attribute>
                        <xsl:call-template name="analyse-pauses">
                                <xsl:with-param name="times" select="$times/@compact"/>
                        </xsl:call-template>
                </xsl:element>
			
		    <xsl:element name="tuple-data" use-attribute-sets="time-axes">
                <xsl:attribute name="label">Pause times</xsl:attribute>
                        <xsl:call-template name="analyse-pauses">
                               <xsl:with-param name="times" select="$gcs/time/@totalms"/>
                        </xsl:call-template>
                </xsl:element>
		
		                <xsl:element name="tuple-data" use-attribute-sets="x-axis">
                <xsl:attribute name="label">Allocation failures</xsl:attribute>
                        <xsl:attribute name="y-axis">allocation failure</xsl:attribute>
                        <xsl:attribute name="y-units">#</xsl:attribute>
                        <xsl:call-template name="analyse-by-criteria">
                                <xsl:with-param name="entries" select="$gcs"/>
                                <xsl:with-param name="name" select="'af'"/>
                        </xsl:call-template>
                </xsl:element>
  
			
		
		</run>
		</variant>
	</xsl:template>
	<xsl:template name="analyse-pauses">
		<xsl:param name="times"/>
		<!-- now make a record of all the pauses -->
			<xsl:for-each select="$times">
				<xsl:element name="data-point">
				<xsl:attribute name="label"></xsl:attribute>
					<xsl:attribute name="x">
						<xsl:value-of select="ancestor::*[@timestamp]/@timestamp"/>
					</xsl:attribute>
					<xsl:attribute name="y">
						<xsl:value-of select="."/>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
	</xsl:template>
	<xsl:template name="analyse-sizes">
		<xsl:param name="sizes"/>
		<!-- now make a record of all the pauses -->
			<xsl:for-each select="$sizes">
				<xsl:element name="data-point">
				<xsl:attribute name="label"></xsl:attribute>
					<xsl:attribute name="x">
						<xsl:value-of select="ancestor::*[@timestamp]/@timestamp"/>
					</xsl:attribute>
					<xsl:attribute name="y">
						<xsl:value-of select=". div (1024 * 1024)"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
	</xsl:template>

		<xsl:template name="analyse-by-criteria">
		<xsl:param name="entries"/>
	<xsl:param name="name"/>
	
			<xsl:for-each select="$entries">
				<xsl:element name="data-point">
				<xsl:attribute name="label"></xsl:attribute>
					<xsl:attribute name="x">
						<xsl:value-of select="ancestor-or-self::*[@timestamp]/@timestamp"/>
					</xsl:attribute>
					<xsl:attribute name="y">
					<xsl:choose>	
			<xsl:when test="name(.)=$name">1.0</xsl:when>
			<xsl:otherwise>0</xsl:otherwise> 
			</xsl:choose>
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
	</xsl:template>
	
</xsl:stylesheet>
